package org.atguigu.controller;

import org.atguigu.util.LoginInfoUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.Principal;

/**
 * @Author 海晨忆
 * @Des
 * @Date 2025/8/1 13:23
 */
@Controller
public class UserController {

    @RequestMapping(value = "/")
    public @ResponseBody String index() {
        return "Welcome to Spring Security";
    }

    @RequestMapping(value = "/toLogin")
    public String toLogin() {
        return "login";
    }

    /**
     * 获取登录用户信息
     * 方式一：Principal
     *
     * @param principal
     * @return
     */
    @RequestMapping(value = "/welcome")
    public @ResponseBody Object welcome(Principal principal) {
        return principal;
    }

    /**
     * 获取登录用户信息
     * 方式二：Authentication
     *
     * @param authentication
     * @return
     */
    @RequestMapping(value = "/welcome2")
    public @ResponseBody Object welcome2(Authentication authentication) {
        return authentication;
    }

    /**
     * 获取登录用户信息
     * 方式三：UsernamePasswordAuthenticationToken
     *
     * @param usernamePasswordAuthenticationToken
     * @return
     */
    @RequestMapping(value = "/welcome3")
    public @ResponseBody Object welcome3(UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) {
        return usernamePasswordAuthenticationToken;
    }

    /**
     * 获取登录用户信息
     * 方式五：SecurityContextHolder.getContext().getAuthentication()
     *
     * @return
     */
    @RequestMapping(value = "/welcome4")
    public @ResponseBody Object welcome4() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取当前登录的用户信息
     *
     * @return
     */
    @RequestMapping(value = "/welcome5")
    public @ResponseBody Object welcome5() {
        return LoginInfoUtil.getCurrentLoginUser();
    }
}
